package com.ybear.ybutils.utils.dialog;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.SearchEvent;
import android.view.View;
import android.view.Window;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class DialogOption {
    private final android.app.Dialog mDialog;
    public DialogOption(android.app.Dialog dialog) { mDialog = dialog; }

    public android.app.Dialog get() { return mDialog; }

    public void show() { post( 0 ); }

    public void hide() { post( 1 ); }

    public void cancel() { post( 2 ); }

    public void dismiss() { post( 3 ); }

    public boolean isShowing() { return this.mDialog != null && this.mDialog.isShowing(); }

    public void setDismissMessage(@Nullable Handler handler, @Nullable Message msg) {
        if( mDialog == null ) return;
        if( handler != null && msg != null ) msg.setTarget( handler );
        mDialog.setDismissMessage( msg );
    }

    public void setDismissMessage(@Nullable Message msg) { setDismissMessage( null, msg ); }

    public void setCancelMessage(Handler handler, @Nullable Message msg) {
        if( mDialog == null ) return;
        if( handler != null && msg != null ) msg.setTarget( handler );
        mDialog.setCancelMessage( msg );
    }

    public void setCancelMessage(@Nullable Message msg) { setCancelMessage( null, msg ); }

    public DialogOption setOnCancelListener(DialogInterface.OnCancelListener l) {
        if( mDialog != null ) mDialog.setOnCancelListener( l );
        return this;
    }

    public DialogOption setOnDismissListener(DialogInterface.OnDismissListener l) {
        if( mDialog != null ) mDialog.setOnDismissListener( l );
        return this;
    }

    public DialogOption setOnShowListener(DialogInterface.OnShowListener l) {
        if( mDialog != null ) mDialog.setOnShowListener( l );
        return this;
    }

    public DialogOption setOnKeyListener(DialogInterface.OnKeyListener l) {
        if( mDialog != null ) mDialog.setOnKeyListener( l );
        return this;
    }

    public DialogOption setCanceledOnTouchOutside(boolean enable) {
        if( mDialog != null ) mDialog.setCanceledOnTouchOutside( enable );
        return this;
    }

    public DialogOption setCancelable(boolean enable) {
        if( mDialog != null ) mDialog.setCancelable( enable );
        return this;
    }

    public DialogOption setVolumeControlStream(int streamType) {
        if( mDialog != null ) mDialog.setVolumeControlStream( streamType );
        return this;
    }

    public DialogOption setOwnerActivity(@NonNull Activity activity) {
        if( mDialog != null ) mDialog.setOwnerActivity( activity );
        return this;
    }

    public Activity getOwnerActivity() { return mDialog.getOwnerActivity(); }

    public int getVolumeControlStream() { return mDialog.getVolumeControlStream(); }

    public Context getContext() { return mDialog.getContext(); }

    public View getCurrentFocus() { return mDialog.getCurrentFocus(); }

    public Window getWindow() { return mDialog.getWindow(); }

    public LayoutInflater getLayoutInflater() { return mDialog.getLayoutInflater(); }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public SearchEvent getSearchEvent() { return mDialog.getSearchEvent(); }

    public ActionBar getActionBar() { return mDialog.getActionBar(); }

    @Nullable
    public <T extends View> T findViewById(@IdRes int id) {
        return mDialog != null ? mDialog.findViewById( id ) : null;
    }

    private void post(int what) {
        if( mDialog == null || getWindow() == null ) return;
        try {
            switch ( what ) {
                case 0:
                    mDialog.show();
                    break;
                case 1:
                    mDialog.hide();
                    break;
                case 2:
                    mDialog.cancel();
                    break;
                case 3:
                    mDialog.dismiss();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}